package cn.master.volley.models.response.listener;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.type.TypeFactory;

import java.io.IOException;
import java.util.HashMap;

import cn.master.volley.commons.JacksonJsonUtil;
import cn.master.volley.models.pojo.Wrapper;

/**
 * Json 转换助手类
 */
public class ResolveJsonHelper {
	/**
	 * 通过 {@link Wrapper} 解析 json 数据，其中 {@link Wrapper#getData()} 以  {@link String} 方式解析
	 */
	public static ResolveJson<String> resolveJson2String = new ResolveJson<String>() {
		/**
		 *  中 {@link ResolveJson} 的默认实现 .</br>
		 * 用 {@link String} 方式获取 {@link Wrapper#getData()} 数据
		 * @param respJson
		 * @return
		 */
		@Override
		public Wrapper<String> resolve(String respJson)
				throws IOException {
			
			JavaType javaType = TypeFactory.defaultInstance()
					.constructParametricType(Wrapper.class, String.class);
			return JacksonJsonUtil.getObjectMapper().readValue(respJson,
					javaType);


		}
	};
	
	/**
	 * 通过 {@link Wrapper} 解析 json 数据，其中 {@link Wrapper#getData()} 以  {@link Integer} 方式解析
	 */
	public static ResolveJson<Integer> resolveJson2Integer = new ResolveJson<Integer>() {
		/**
		 * 用 {@link Integer} 方式获取 {@link Wrapper#getData()} 数据
		 * @param respJson
		 * @return
		 */
		@Override
		public Wrapper<Integer> resolve(String respJson)
				throws IOException {
			
			JavaType javaType = TypeFactory.defaultInstance()
					.constructParametricType(Wrapper.class, Integer.class);
			return JacksonJsonUtil.getObjectMapper().readValue(respJson,
					javaType);
		}
	};
	
	public static <E extends Wrapper<T>,T> ResolveJson<T> resolveJson2POJO(final Class<E> clazz){
		return new ResolveJson<T>() {
			@Override
			public E resolve(String respJson)
					throws
					IOException {
				return JacksonJsonUtil.getObjectMapper().readValue(respJson, clazz);
			}
		};
	}
	
	/**
	 * 通过 {@link Wrapper} 解析 json 数据，解析时忽略 {@link Wrapper#getData()} 数据.
	 */
	public static ResolveJson<Object> resolveAndIgnoreData = new ResolveJson<Object>() {
		/**
		 * 用 {@link Object} 方式获取 {@link Wrapper#getData()} 数据
		 * @param respJson
		 * @return
		 */
		@Override
		public Wrapper<Object> resolve(String respJson)
				throws IOException {
			
			JavaType javaType = TypeFactory.defaultInstance()
					.constructParametricType(Wrapper.class, Object.class);
			return JacksonJsonUtil.getObjectMapper().readValue(respJson,
					javaType);
		}
	};
	/**
	 * 通过 {@link Wrapper} 解析 json 数据，解析时 {@link Wrapper#getData()} 为{@link HashMap}.
	 */
	public static ResolveJson<HashMap<String,Integer>> resolve2HashMapInteger = new ResolveJson<HashMap<String,Integer>>() {
		/**
		 * 用 {@link HashMap} 方式获取 {@link Wrapper#getData()} 数据
		 * @param respJson
		 * @return
		 * @throws IOException 
		 * @throws JsonMappingException 
		 * @throws JsonParseException 
		 */
		@Override
		public Wrapper<HashMap<String, Integer>> resolve(String respJson)
				throws IOException {
			TypeReference<Wrapper<HashMap<String, Integer>>> typeReference = new TypeReference<Wrapper<HashMap<String, Integer>>>() {};
			return JacksonJsonUtil.getObjectMapper().readValue(respJson,
					typeReference);
		}
	};
	
	/**
	 * 通过 {@link Wrapper} 解析 json 数据，解析时 {@link Wrapper#getData()} 为{@link HashMap}.
	 */
	public static ResolveJson<HashMap<String,String>> resolve2HashMapString = new ResolveJson<HashMap<String,String>>() {
		/**
		 * 用 {@link HashMap} 方式获取 {@link Wrapper#getData()} 数据
		 * @param respJson
		 * @return
		 * @throws IOException 
		 * @throws JsonMappingException 
		 * @throws JsonParseException 
		 */
		@Override
		public Wrapper<HashMap<String, String>> resolve(String respJson)
				throws IOException {
			TypeReference<Wrapper<HashMap<String, String>>> typeReference = new TypeReference<Wrapper<HashMap<String, String>>>() {};
			return JacksonJsonUtil.getObjectMapper().readValue(respJson,
					typeReference);
		}
	};
}
